//
//  XHRFeedBackController.m
//  快医
//
//  Created by 孙博 on 16/7/6.
//
/**
 *  反馈的页面
 */

#import "XHRFeedBackController.h"
#import "SVProgressHUD.h"

@interface XHRFeedBackController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,UITextViewDelegate>

@property (nonatomic, weak)UITextView *tv_feedBack;

@property (nonatomic, weak)UIView *bgView;

@property (nonatomic, weak)UILabel *headLabel;

@property (nonatomic, weak)UIButton *commitBtn;

@property (nonatomic, weak)UITableView *table;

// 假装placeHolder的label
@property (nonatomic, weak)UILabel *faker;

// 记录字数的label
@property (nonatomic, weak)UILabel *label;

// 记录剩余字数
@property (nonatomic, assign)NSInteger words;

@end

@implementation XHRFeedBackController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupUI];
    
    [self setupLayout];
    
    self.navigationItem.title = @"";
}

- (void)setupUI{
    
    
    UITextView *tv = [UITextView new];
    
    tv.font = [UIFont systemFontOfSize:16];
    tv.delegate = self;
    tv.backgroundColor = [UIColor lightGrayColor];
    self.tv_feedBack = tv;
    [self.view addSubview:tv];
    
    // 设置placeholder
    /**
     *  在textView中添加一个label
     */
    UILabel *lb = [[UILabel alloc]initWithFrame:CGRectZero];
    lb.textColor = [UIColor grayColor];
    lb.text = @"请输入反馈信息:";
    [lb sizeToFit];
    self.faker = lb;
    [tv addSubview:lb];
    
    // bgView
    UIView *bgView =[UIView new];
    self.bgView = bgView;
    [self.view addSubview:bgView];
    
    // MARK:  在bgView上添加btn label
    UIButton *btn = [[UIButton alloc]init];
    [btn setTitle:@"提交反馈" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.backgroundColor = XHRThemeColor;
    [btn sizeToFit];
    btn.frame = CGRectMake(XHRScreenWidth - 10 - btn.xhr_Width, 5, btn.xhr_Width, btn.xhr_Height);
    btn.layer.cornerRadius = 3;
    btn.layer.masksToBounds = YES;
    [btn addTarget:self action:@selector(clickFeedBack) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:btn];
    
    UILabel *label = [UILabel new];
    [bgView addSubview:label];
    label.textAlignment = NSTextAlignmentRight;
    label.text = [NSString stringWithFormat:@"当前剩余字数为:"];
    [label makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(bgView.centerY);
        make.leading.equalTo(btn.leading).offset(-170);
    }];
    
    UILabel *wordLabel = [UILabel new];
    [bgView addSubview:wordLabel];
    [wordLabel makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(btn.leading).offset(-10);
        make.centerY.equalTo(bgView.centerY);
    }];
    self.words = 200;
    wordLabel.text = [NSString stringWithFormat:@"%ld",self.words];
    self.label = wordLabel;
    
    
    
    // headLabel
    UILabel *headLabel = [UILabel new];
    headLabel.backgroundColor = [UIColor lightGrayColor];
    self.headLabel = headLabel;
    headLabel.textColor = [UIColor blackColor];
    headLabel.font = [UIFont systemFontOfSize:16];
    headLabel.text = @"     欢迎联系我们";
    [self.view addSubview:headLabel];
    
    // table
    UITableView *table = [UITableView new];
    table.dataSource = self;
    table.delegate = self;
    table.tableFooterView = [UIView new];
    [table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"feedBack"];
    
    self.table = table;
    [self.view addSubview:table];
}

- (void)setupLayout{
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.tv_feedBack makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(5);
        make.leading.equalTo(10);
        make.trailing.equalTo(-10);
        make.height.equalTo(180);
    }];
    
    [self.bgView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tv_feedBack.bottom);
        make.leading.trailing.equalTo(0);
        make.height.equalTo(40);
    }];
    
    [self.headLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.bottom);
        make.leading.trailing.equalTo(0);
        make.height.equalTo(40);
    }];
    
    [self.table makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headLabel.bottom);
        make.leading.trailing.equalTo(0);
        make.bottom.equalTo(0);
    }];
}

#pragma
#pragma mark -dtable的代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"feedBack" forIndexPath:indexPath];
    
    if (indexPath.row == 0) {
        cell.imageView.image = [UIImage imageNamed:@"dianhua"];
        cell.textLabel.text = @"官方电话: 135-1551-6762";
    }
    if (indexPath.row == 1) {
        cell.imageView.image = [UIImage imageNamed:@"qq"];
        cell.textLabel.text = @"官方QQ群: 46476588";
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确定拨打电话  135-1551-6762" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

//            进行拨号
            NSURL *url = [NSURL URLWithString:@"tel://13515516762"];
            [[UIApplication sharedApplication]openURL:url];
            
        }];
        
        UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
        
        [alert addAction:sure];
        [alert addAction:cancle];
        
        [self presentViewController:alert animated:YES completion:nil];
    }
    
    if (indexPath.row == 1) {
        [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    }
}

#pragma
#pragma mark -tv的代理
- (NSUInteger)textLengthFromString:(NSString *)string
{
    return [string lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
}

- (void)textViewDidBeginEditing:(UITextView *)textView{

    [self.faker removeFromSuperview];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    NSString *textFieldString = [textView.text stringByReplacingCharactersInRange:range withString:text];
    
    self.words = [self textLengthFromString:textFieldString];
    
    // 刷新label中的字数
    self.label.text = [NSString stringWithFormat:@"%ld", 200 - self.words];
    
    if ([text length] > 0){
        
        if (self.words > 200){
            return NO;
        } else {
            return  YES;
        }
    }
    return YES;
}


#pragma
#pragma mark -点击提交反馈
- (void)clickFeedBack{
    
    // 如果剩余字数仍然是200,无法点击提交
    if (self.words == 200) {
        
        // 在1秒后消失
        [SVProgressHUD setMinimumDismissTimeInterval:1];
        // 添加一个遮罩
        [SVProgressHUD showInfoWithStatus:@"内容为空"];
        // 整个后面的背景选择
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    }
    
    [self.view endEditing:YES];
}

@end
